package com.zj.base.utils;

import android.app.Activity;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.inputmethod.InputMethodManager;

import com.zj.base.base_act.IBaseApplication;


/**
 * Created by zhaojie on 2017/10/10.
 */
@SuppressWarnings("unused")
public class DisplayUtils {

    private static DisplayMetrics displayMetrics;
    private static boolean isInit = false;

    public static void init(Context c) {
        if (isInit) return;
        displayMetrics = c.getResources().getDisplayMetrics();
        isInit = true;
    }

    private static DisplayMetrics getDisplayMetrics() {
        if (displayMetrics == null)
            displayMetrics = IBaseApplication.getAppContext().getResources().getDisplayMetrics();
        return displayMetrics;
    }

    /**
     * 将px值转换为dip或dp值，保证尺寸大小不变
     */
    public static int px2dip(float pxValue) {
        final float scale = getDisplayMetrics().densityDpi;
        return (int) ((pxValue * 160) / scale + 0.5f);
    }

    /**
     * 将dip或dp值转换为px值
     */
    public static int dip2px(float dipValue) {
        final float scale = getDisplayMetrics().densityDpi;
        return (int) (dipValue * (scale / 160) + 0.5f);
    }

    /**
     * 获取屏幕宽高
     */
    public static int[] getDefaultDisplayMetrics() {
        int[] i = new int[2];
        i[0] = getDisplayMetrics().widthPixels;
        i[1] = getDisplayMetrics().heightPixels;
        return i;
    }

    public static int getScreenWidth() {
        DisplayMetrics dm = getDisplayMetrics();
        return dm.widthPixels;
    }

    //隱藏軟鍵盤
    public static void hiddenInputMethod(Context context) {
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null && imm.isActive()) {
            if (context instanceof Activity) {
                imm.hideSoftInputFromWindow(((Activity) context).getWindow().getDecorView().getWindowToken(), 0);
            }
        }
    }
}

